package 面试题18_1_O1时间内删除链表的节点;

/**
 * @Author ：xu_xiaofeng.
 * @Date ：Created in 23:04 2021/2/15
 * @Description：
 */

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

public class Solution {
    public ListNode deleteNode(ListNode head, int val) {

        // 链表只有一个节点
        if (head.val == val) {
            return head.next;
        }

        ListNode preNode = head;
        ListNode curNode = head.next;

        while (curNode != null) {
            if (curNode.val == val) {
                preNode.next = curNode.next;
                break;
            }

            preNode = curNode;
            curNode = curNode.next;
        }

        return head;
    }
}
